﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Service;
using Cashier.utils;
using Cashier_Utils;
using Cashier_Model;
using System.Collections;
using Cashier.prints;
using Cashier.member;
using Cashier_Interface;

namespace Cashier.pos
{
    public partial class SimplePosForm : Form, IReloadData
    {
        private MainForm mainform;
        private GoodsService goodsService;
        private DataTable goodsTable;
        private MemberService memberService;
        private Member member;
        private BillShipmentService shipmentService;
        private PosBillPrintDoc posBillPrintDoc;
        private static SimplePosForm simplePosForm;
        private String shipmentno;
        private int textboxSaleFocusCount;

        private SimplePosForm(MainForm mainform)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.goodsService = new GoodsService();
            this.memberService = new MemberService();
            this.shipmentService = new BillShipmentService();

            this.InitialListBox();
            this.GeneratorDataGridViewColumns();
            this.textbox_priceSale.GotFocus += new EventHandler(textbox_priceSale_GotFocus);
            this.textbox_memberNo.LostFocus += new EventHandler(textbox_memberNo_LostFocus);
            this.textbox_usedBalance.LostFocus += new EventHandler(textbox_usedBalance_LostFocus);
            this.textbox_payAmount.LostFocus += new EventHandler(textbox_payAmount_LostFocus);
        }

        /// <summary>
        /// 获取SimplePosForm实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <returns></returns>
        public static SimplePosForm GetSimplePosForm(MainForm mainform)
        {
            if (simplePosForm == null)
                simplePosForm = new SimplePosForm(mainform);

            return simplePosForm;
        }

        #region 初始化

        /// <summary>
        /// 初始ListBox数据
        /// </summary>
        private void InitialListBox()
        {
            try
            {
                listBox1.DrawMode = DrawMode.OwnerDrawVariable;
                listBox1.DisplayMember = "name";
                listBox1.ValueMember = "id";
                goodsTable = goodsService.GetGoodsTable(0, 0, null, null, 0, 0, null, 0, "id", null);
                listBox1.DataSource = goodsTable;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 生成DataGridView列
        /// </summary>
        protected void GeneratorDataGridViewColumns()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("barcode", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_name", "商品名称", true, 30, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("category_name", "商品分类", true, 12, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_norm", "商品规格", true, 12, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("price_cost", "进货单价", true, 12, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("price_sale", "销售单价", true, 12, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("numbers", "数量", true, 10, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("total", "合计", true, 12, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_id", null, true, 0, null, null, false, false));
        }

        #endregion

        #region 功能方法实现

        /// <summary>
        /// DataGridView添加行
        /// </summary>
        private void DataGridViewAddRow()
        {
            try
            {
                if (goodsTable != null)
                {
                    String id = listBox1.SelectedValue.ToString();
                    DataRow row = goodsTable.Select("id=" + id)[0];
                    int rowIndex = dataGridView1.Rows.Add();
                    dataGridView1.Rows[rowIndex].Cells["goods_id"].Value = row["id"];
                    dataGridView1.Rows[rowIndex].Cells["barcode"].Value = row["barcode"];
                    dataGridView1.Rows[rowIndex].Cells["goods_name"].Value = row["name"];
                    dataGridView1.Rows[rowIndex].Cells["category_name"].Value = row["categoryname"];
                    dataGridView1.Rows[rowIndex].Cells["goods_norm"].Value = row["norm"];
                    decimal priceCost = Decimal.Parse(textbox_priceCost.Text.Trim());
                    decimal priceSale = Decimal.Parse(textbox_priceSale.Text.Trim());
                    int numbers = Int32.Parse(textbox_numbers.Text.Trim());
                    dataGridView1.Rows[rowIndex].Cells["price_cost"].Value = priceCost;
                    dataGridView1.Rows[rowIndex].Cells["price_sale"].Value = priceSale;
                    dataGridView1.Rows[rowIndex].Cells["numbers"].Value = numbers;
                    dataGridView1.Rows[rowIndex].Cells["total"].Value = priceSale * numbers;
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 计算帐单
        /// </summary>
        private void CountShipment()
        {
            if (dataGridView1.Rows.Count > 0)
            {
                int numbers = 0;
                decimal payableAmount = 0M;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    numbers += Int32.Parse(row.Cells["numbers"].Value.ToString());
                    decimal countTotal = (decimal)row.Cells["total"].Value;
                    payableAmount += countTotal;
                }
                label_numbers.Text = numbers + "";
                label_goodsTotal.Text = payableAmount.ToString("f2");

                if (!String.IsNullOrEmpty(textbox_usedBalance.Text.Trim()))
                {
                    decimal usedBalance = Decimal.Parse(textbox_usedBalance.Text.Trim());
                    if (payableAmount >= usedBalance)
                        payableAmount -= usedBalance;
                    else
                        payableAmount = 0;
                }

                label_payableAmount.Text = payableAmount.ToString("f2");
            }
            else
            {
                label_numbers.Text = "0";
                label_goodsTotal.Text = "0.00";
                label_payableAmount.Text = "0.00";
            }
        }

        /// <summary>
        /// 结帐
        /// </summary>
        private void ShipmentAccount()
        {
            try
            {
                if (dataGridView1.Rows.Count == 0)
                {
                    MessageBox.Show("请购买商品后再结账！");
                    return;
                }

                decimal balance = Decimal.Parse(label_balance.Text);
                decimal usedBalance = Decimal.Parse(textbox_usedBalance.Text.Trim());
                decimal goodsTotal = Decimal.Parse(label_goodsTotal.Text);
                decimal payableAmount = Decimal.Parse(label_payableAmount.Text);
                decimal payAmount = Decimal.Parse(textbox_payAmount.Text.Trim());
                decimal giveAmount = Decimal.Parse(label_giveAmount.Text);
                decimal debtAmount = Decimal.Parse(label_debtAmount.Text);

                if (balance > 0)
                {
                    String memberno = textbox_memberNo.Text.Trim();
                    if (String.IsNullOrEmpty(memberno) || this.member == null || 
                        (member!=null && !member.Memberno.Equals(memberno)))
                    {
                        MessageBox.Show("会员编号有误！");
                        textbox_memberNo.Focus();
                        return;
                    }                    

                    if (usedBalance > balance)
                    {
                        MessageBox.Show("卡内余额不足！");
                        textbox_usedBalance.Focus();
                        return;
                    }
                    else
                    {
                        if (usedBalance > goodsTotal)
                        {
                            MessageBox.Show("使用余额超过商品金额，请重新输入！");
                            textbox_usedBalance.Focus();
                            return;
                        }
                    }
                }
                else
                {
                    if (usedBalance > 0)
                    {
                        MessageBox.Show("卡内余额不足！");
                        textbox_usedBalance.Focus();
                        return;
                    }
                }

                payableAmount = goodsTotal - usedBalance;
                label_payableAmount.Text = payableAmount.ToString("f2");

                if (!Config.IS_POS_DEBT && payAmount < payableAmount)
                {
                    MessageBox.Show("支付金额不足！");
                    textbox_payAmount.Focus();
                    return;
                }

                if ((payableAmount - payAmount) > 0)
                    debtAmount = payableAmount - payAmount;
                else
                    debtAmount = 0;
                label_debtAmount.Text = debtAmount.ToString("f2");

                //保存出货单
                BillShipment billShipment = new BillShipment();
                billShipment.EnterpriseNo = mainform.Admin.EnterpriseNo;
                billShipment.StoreNo = mainform.Admin.StoreNo;
                shipmentno = ToolsUtil.GenerateShipmentNO();
                billShipment.Shipmentno = shipmentno;
                billShipment.Memberno = member != null ? member.Memberno : string.Empty;
                billShipment.Numbers = 0;
                billShipment.GoodsTotal = goodsTotal;
                billShipment.ShipmentTotal = goodsTotal;
                billShipment.DiscountType = String.Empty;
                billShipment.DiscountAmount = 0;
                billShipment.DiscountTotal = 0;
                billShipment.MemberBalance = usedBalance;
                billShipment.PayableAmount = payableAmount;
                billShipment.Payway = (int)Payway.Cash;
                billShipment.PayAmount = payAmount;
                billShipment.GiveAmount = giveAmount;
                billShipment.DebtAmount = debtAmount;
                billShipment.ShipmentDate = DateTime.Now;
                billShipment.Note = textbox_note.Text.Trim();
                billShipment.AdminUsername = mainform.Admin.Username;
                billShipment.AdminName = mainform.Admin.Name;
                billShipment.Createtime = DateTime.Now;

                int goodsNumbers = 0;
                ArrayList list = new ArrayList();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    BillShipmentGoods shipmentGoods = new BillShipmentGoods();
                    shipmentGoods.EnterpriseNo = billShipment.EnterpriseNo;
                    shipmentGoods.StoreNo = billShipment.StoreNo;
                    shipmentGoods.Shipmentno = billShipment.Shipmentno;
                    shipmentGoods.GoodsId = (long)row.Cells["goods_id"].Value;
                    shipmentGoods.Barcode = row.Cells["barcode"].Value.ToString();
                    shipmentGoods.CategoryName = row.Cells["category_name"].Value.ToString();
                    shipmentGoods.GoodsName = row.Cells["goods_name"].Value.ToString();
                    shipmentGoods.GoodsNorm = row.Cells["goods_norm"].Value.ToString();
                    shipmentGoods.PriceCost = (decimal)row.Cells["price_cost"].Value;
                    shipmentGoods.PriceSale = (decimal)row.Cells["price_sale"].Value;
                    shipmentGoods.Discount = 1;
                    shipmentGoods.PriceDisc = shipmentGoods.PriceSale;
                    shipmentGoods.Numbers = (int)row.Cells["numbers"].Value;
                    shipmentGoods.Total = (decimal)row.Cells["total"].Value;
                    shipmentGoods.Paid = shipmentGoods.Total;
                    shipmentGoods.Debt = 0M;
                    shipmentGoods.AdminUsername = billShipment.AdminUsername;
                    shipmentGoods.AdminName = billShipment.AdminName;
                    shipmentGoods.Createtime = DateTime.Now;

                    goodsNumbers += shipmentGoods.Numbers;
                    list.Add(shipmentGoods);
                }
                billShipment.Numbers = goodsNumbers;

                //会员卡消费记录
                MemberMoney memberMoney = null;              
                if (balance > 0 && member!=null)
                {
                    memberMoney = new MemberMoney();
                    memberMoney.EnterpriseNo = billShipment.EnterpriseNo;
                    memberMoney.StoreNo = billShipment.StoreNo;
                    memberMoney.TransNo = GuidUtil.GetGuidToLong().ToString();
                    memberMoney.MemberNo = member.Memberno;
                    memberMoney.Money = usedBalance;
                    memberMoney.LastMoney = member.Balance - usedBalance;
                    memberMoney.MoneyTime = DateTime.Now;
                    memberMoney.Note = "出货单号：" + billShipment.Shipmentno;
                    memberMoney.AdminUsername = billShipment.AdminUsername;
                    memberMoney.AdminName = billShipment.AdminName;
                    memberMoney.CreateTime = DateTime.Now;
                }

                long id = this.shipmentService.InsertBillShipment(billShipment, list, memberMoney);
                if (id > 0)
                {
                    //存入打印容器中
                    this.posBillPrintDoc = new PosBillPrintDoc(billShipment, list);
                    this.PrintPosBill(); //打印小票
                    this.RestorePosForm();
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 打开钱箱
        /// </summary>
        private void OpenMoneybox()
        {
            String printerName = printDocument1.PrinterSettings.PrinterName;
            //标准 
            //String send = "" + (char)(27) + (char)(64) + (char)(27) + 'J' + (char)(255);   
            //非标准   
            String send = "" + (char)(27) + (char)(112) + (char)(0) + (char)(60) + (char)(255);
            RawPrinterHelper.SendStringToPrinter(printerName, send);
        }

        /// <summary>
        /// 打印POS出货小票
        /// </summary>
        private void PrintPosBill()
        {
            if (Config.TICKET_PRINT_PREVIEW)
            {
                PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
                printPreviewDialog1.Document = printDocument1;
                printPreviewDialog1.PrintPreviewControl.Zoom = 1;
                printPreviewDialog1.ShowDialog();
            }
            else
            {
                this.OpenMoneybox();
                printDocument1.Print();
            }
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        private void RestorePosForm()
        {
            textboxSaleFocusCount = 0;
            member = null;
            dataGridView1.Rows.Clear();
            textbox_memberNo.Text = String.Empty;
            label_memberName.Text = String.Empty;
            label_balance.Text = "0.00";
            textbox_usedBalance.Text = "0.00";
            textbox_usedBalance.Enabled = false;
            label_numbers.Text = "0";
            label_goodsTotal.Text = "0.00";
            textbox_note.Text = String.Empty;
            label_payableAmount.Text = "0.00";
            textbox_payAmount.Text = "0.00";
            label_giveAmount.Text = "0.00";
            label_debtAmount.Text = "0.00";
            listBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// ListBox获得焦点事件
        /// </summary>
        /// <param name="keyData"></param>
        private void ListBoxFocus(Keys keyData)
        {
            int index = listBox1.SelectedIndex;
            int itemCount = listBox1.Items.Count;

            if (keyData == Keys.Up)
            {
                if (index > 0)
                    listBox1.SelectedIndex = index - 1;
            }
            else if (keyData == Keys.Down)
            {
                if (index < (itemCount - 1))
                {
                    listBox1.SelectedIndex = index + 1;
                }
            }
        }

        #endregion

        #region ListBox事件

        /// <summary>
        /// 选择项改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textboxSaleFocusCount = 0;
            DataRowView rowView = listBox1.SelectedItem as DataRowView;
            if (rowView != null)
            {
                Object[] objs = rowView.DataView.Table.Rows[listBox1.SelectedIndex].ItemArray;
                label_productName.Text = objs[3].ToString();
                label_norm.Text = objs[5] != null ? objs[5].ToString() : String.Empty;
                textbox_priceCost.Text = ((decimal)objs[6]).ToString("f2");
                textbox_priceSale.Text = ((decimal)objs[7]).ToString("f2");
                textbox_numbers.Text = "1";

                textbox_priceCost.Focus();
            }
        }

        /// <summary>
        /// 用户编号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_memberNo_LostFocus(object sender, EventArgs e)
        {
            label_memberName.Text = String.Empty;
            label_balance.Text = "0.00";
            textbox_usedBalance.Text = "0.00";
            textbox_usedBalance.Enabled = false;
            bool isRollback = true;

            String memberno = textbox_memberNo.Text.Trim();
            if (!String.IsNullOrEmpty(memberno))
            {
                this.member = memberService.GetMember(0, memberno);
                if (this.member != null)
                {
                    if (this.member.IsLock)
                    {
                        MessageBox.Show("会员已锁定：" + this.member.LockReason);
                        textbox_memberNo.Text = String.Empty;
                        return;
                    }
                    else
                    {
                        label_memberName.Text = this.member.Name;
                        if (this.member.Balance > 0)
                        {
                            textbox_usedBalance.Enabled = true;
                            label_balance.Text = this.member.Balance.ToString("f2");
                            //商品总金额
                            decimal goodsTotal = Decimal.Parse(label_goodsTotal.Text);
                            if (goodsTotal > 0)
                            {
                                if (this.member.Balance <= goodsTotal)
                                    textbox_usedBalance.Text = this.member.Balance.ToString("f2");
                                else
                                    textbox_usedBalance.Text = goodsTotal.ToString("f2");

                                //应付金额
                                label_payableAmount.Text = (goodsTotal - Decimal.Parse(textbox_usedBalance.Text.Trim())).ToString("f2");
                                textbox_usedBalance.Focus();

                                isRollback = false;
                            }
                        }
                    }
                }
            }

            if (isRollback)
                label_payableAmount.Text = label_goodsTotal.Text;
        }

        /// <summary>
        /// 卡内余额失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_usedBalance_LostFocus(object sender, EventArgs e)
        {
            decimal lastBalance = 0M;
            if (!String.IsNullOrEmpty(label_balance.Text))
                lastBalance = Decimal.Parse(label_balance.Text);

            decimal usedBalance = 0M;
            if(!String.IsNullOrEmpty(textbox_usedBalance.Text.Trim()))
                usedBalance = Decimal.Parse(textbox_usedBalance.Text.Trim());

            if (usedBalance > lastBalance)
            {
                MessageBox.Show("卡内余额不足！");
                return;
            }

            //商品总金额
            decimal goodsTotal = Decimal.Parse(label_goodsTotal.Text);
            if (goodsTotal >= usedBalance)
            {
                //应付金额
                label_payableAmount.Text = (goodsTotal - usedBalance).ToString("f2");
            }
            else
            {
                label_payableAmount.Text = "0.00";
            }
        }

        /// <summary>
        /// 实付金额失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_payAmount_LostFocus(object sender, EventArgs e)
        {
            decimal payableAmount = Decimal.Parse(label_payableAmount.Text);
            decimal payAmount = 0M;
            if (!String.IsNullOrEmpty(textbox_payAmount.Text.Trim()))
                payAmount = Decimal.Parse(textbox_payAmount.Text.Trim());

            if (payAmount > payableAmount)
            {
                label_giveAmount.Text = (payAmount - payableAmount).ToString("f2");
            }
            else if (payAmount < payableAmount)
            {
                label_giveAmount.Text = "0.00";
                if (Config.IS_POS_DEBT)
                    label_debtAmount.Text = (payableAmount - payAmount).ToString("f2");
            }
        }

        /// <summary>
        /// 重绘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            String name = String.Empty;
            DataRowView drv = listBox1.Items[e.Index] as DataRowView;
            if (drv != null)
            {
                Object[] objs = drv.DataView.Table.Rows[e.Index].ItemArray;
                name = objs[3].ToString();
                if (objs[5] != null && !String.IsNullOrEmpty(objs[5].ToString()))
                {
                    name += "/" + objs[5].ToString();
                }
            }
            e.Graphics.DrawString(name, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
        }

        private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            e.ItemHeight = 25;
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SimplePosForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            bool isclose = true;
            if (dataGridView1.Rows.Count > 0)
            {
                DialogResult result = MessageBox.Show("当前有未结算商品，确定要退出吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.No)
                {
                    e.Cancel = true;
                    isclose = false;
                }
            }
            if (isclose)
                simplePosForm = null;
        }

        #endregion

        #region 控制事件

        /// <summary>
        /// 出售价输入框获得焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_priceSale_GotFocus(object sender, EventArgs e)
        {
            if (textboxSaleFocusCount==0)
                textbox_priceSale.Text = textbox_priceCost.Text;
            textboxSaleFocusCount += 1;
        }

        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_buy_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(label_productName.Text.Trim()))
            {
                MessageBox.Show("请选择购买商品！");
                return;
            }
            if (String.IsNullOrEmpty(textbox_numbers.Text.Trim()) ||
                Int32.Parse(textbox_numbers.Text.Trim())<=0)
            {
                MessageBox.Show("请输入商品数量 ！");
                return;
            }
            this.DataGridViewAddRow();
            this.CountShipment();
        }

        /// <summary>
        /// 结帐事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_shipmentAccount_Click(object sender, EventArgs e)
        {
            this.ShipmentAccount();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DialogResult result = MessageBox.Show("确定要删除选中商品吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                    this.CountShipment();
                }
            }
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_clear_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                DialogResult result = MessageBox.Show("确定要删除所有商品吗？", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dataGridView1.Rows.Clear();
                    this.CountShipment();
                }
            }
        }

        /// <summary>
        /// 新增会员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_addMember_Click(object sender, EventArgs e)
        {
            MemberAddForm memberAddForm = new MemberAddForm(mainform, this, textbox_memberNo.Text.Trim());
            memberAddForm.ShowDialog();
        }

        #endregion

        #region 重写事件

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Up:
                    this.ListBoxFocus(Keys.Up);
                    break;
                case Keys.Down:
                    this.ListBoxFocus(Keys.Down);
                    break;
            }
            return b;
        }

        #endregion

        #region 打印

        /// <summary>
        /// 打印POS小票
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (this.posBillPrintDoc != null)
            {
                this.posBillPrintDoc.Print(shipmentno, mainform.Admin.Name + "  " + mainform.Admin.Mobile, e);
            }
        }

        #endregion


        #region IReloadData 成员

        void IReloadData.ReloadDataGridViewData(object param)
        {
            if (param != null)
            {
                String memberno = param.ToString();
                member = memberService.GetMember(0, memberno);
                if (member != null)
                {
                    textbox_memberNo.Text = member.Memberno;
                    label_memberName.Text = member.Name;
                    label_balance.Text = member.Balance.ToString("f2");

                    if (member.Balance > 0)
                        textbox_usedBalance.Enabled = true;
                }
            }
        }

        #endregion
    }
}
